package com.dces.jx.billtest.xwManageHead.service;
import com.dces.jx.billtest.dto.exgCurrManage.DcBillDeleteLogParam;
import com.xdo.domain.PageParam;
import com.dces.jx.billtest.dto.xwManageHead.*;
import com.xdo.common.token.UserInfoToken;
import com.xdo.domain.ResultObject;
import com.xdo.common.base.service.BaseService;
import com.dces.jx.billtest.xwManageHead.dao.DcBillHeadXwtMapper;
import com.dces.jx.billtest.xwManageHead.mapper.DcBillHeadXwtDtoMapper;
import com.dces.jx.billtest.xwManageHead.model.DcBillHeadXwt;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jdk.nashorn.internal.runtime.options.Option;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;
import javax.annotation.Resource;
import java.beans.Transient;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
/**
 * generated by Generate 神码
 * Service;
 *
 * @author xdoV2.0
 * @date: 2021-9-7
 */
@Service
public class DcBillHeadXwtService extends BaseService<DcBillHeadXwt> {
    @Resource
    private DcBillHeadXwtMapper dcBillHeadXwtMapper;
    @Resource
    private DcBillHeadXwtDtoMapper dcBillHeadXwtDtoMapper;
    private List<DcBillCodeItemDto> rotateType;

    @Override
    public Mapper<DcBillHeadXwt> getMapper() {
        return dcBillHeadXwtMapper;
    }
    /**
     * 获取分页信息
     *
     * @author zuzhu
     * @param dcBillHeadXwtParam
     * @param pageParam
     * @return
     */
    public ResultObject<List<DcBillHeadXwtDto>> getListPaged(DcBillHeadXwtParam dcBillHeadXwtParam, PageParam pageParam) {
        // 启用分页查询
        DcBillHeadXwt dcBillHeadXwt = dcBillHeadXwtDtoMapper.toPo(dcBillHeadXwtParam);
        Page<DcBillHeadXwt> page = PageHelper.startPage(pageParam.getPage(), pageParam.getLimit(), pageParam.getSortOrderContent())
                .doSelectPage(() -> dcBillHeadXwtMapper.getList(dcBillHeadXwt));
        List<DcBillHeadXwtDto> dcBillHeadXwtDtos = page.getResult().stream().map(head -> {
            DcBillHeadXwtDto dto = dcBillHeadXwtDtoMapper.toDto(head);
            return dto;
        }).collect(Collectors.toList());
		ResultObject<List<DcBillHeadXwtDto>> paged = ResultObject.createInstance(dcBillHeadXwtDtos, (int) page.getTotal(), page.getPageNum());
        return paged;
    }
    /**
     * 功能描述:新增
     *
     * @param dcBillHeadXwtParam
     * @param userInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public DcBillHeadXwtDto insert(DcBillHeadXwtParam dcBillHeadXwtParam, UserInfoToken userInfo) {
        DcBillHeadXwt dcBillHeadXwt = dcBillHeadXwtDtoMapper.toPo(dcBillHeadXwtParam);
        /**
         * 规范固定字段
         */
        String sid = UUID.randomUUID().toString();
        dcBillHeadXwt.setSid(sid);
        dcBillHeadXwt.setInsertUser(userInfo.getUserNo());
        dcBillHeadXwt.setInsertTime(new Date());
        //设置进口标记默认为I
        dcBillHeadXwt.setIEMark("I");
        //清单申报后系统返填当前用户，默认当前用户
        dcBillHeadXwt.setSendUser(userInfo.getUserNo());
        //录入单位取当前企业的编码
        dcBillHeadXwt.setInputCode(userInfo.getCompany());
        //当前企业的名字
        dcBillHeadXwt.setInputName(userInfo.getCompanyName());
        //当前企业社会信用代码
        dcBillHeadXwt.setInputCreditCode(userInfo.getSocialCreditCode());
        // 新增数据
        int insertStatus = dcBillHeadXwtMapper.insert(dcBillHeadXwt);
        return  insertStatus > 0 ? dcBillHeadXwtDtoMapper.toDto(dcBillHeadXwt) : null;
    }
    /**
     * 功能描述:修改
     *
     * @param dcBillHeadXwtParam
     * @param userInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public DcBillHeadXwtDto update(DcBillHeadXwtParam dcBillHeadXwtParam, UserInfoToken userInfo) {
        DcBillHeadXwt dcBillHeadXwt = dcBillHeadXwtMapper.selectByPrimaryKey(dcBillHeadXwtParam.getSid());
        dcBillHeadXwtDtoMapper.updatePo(dcBillHeadXwtParam, dcBillHeadXwt);
        dcBillHeadXwt.setUpdateUser(userInfo.getUserNo());
        dcBillHeadXwt.setUpdateTime(new Date());
        // 更新数据
        int update = dcBillHeadXwtMapper.updateByPrimaryKey(dcBillHeadXwt);
        return update > 0 ? dcBillHeadXwtDtoMapper.toDto(dcBillHeadXwt) : null;
    }
    /**
     * 功能描述:批量删除
     *
     * @param sids
     * @return
     */
    @Transient
    public void delete(List<String> sids, UserInfoToken userInfo) {
		dcBillHeadXwtMapper.deleteBySids(sids);
    }
    /**
     * 功能描述:查询所有数据
     *
     * @param exportParam
     * @param userInfo
     * @return
     */
    public List<DcBillHeadXwtDto> selectAll(DcBillHeadXwtParam exportParam, UserInfoToken userInfo) {
        DcBillHeadXwt dcBillHeadXwt = dcBillHeadXwtDtoMapper.toPo(exportParam);
        // dcBillHeadXwt.setTradeCode(userInfo.getCompany());
        List<DcBillHeadXwtDto> dcBillHeadXwtDtos = new ArrayList<>();
        List<DcBillHeadXwt> dcBillHeadXwts = dcBillHeadXwtMapper.getList(dcBillHeadXwt);
        if (CollectionUtils.isNotEmpty(dcBillHeadXwts)) {
            dcBillHeadXwtDtos = dcBillHeadXwts.stream().map(head -> {
                DcBillHeadXwtDto dto = dcBillHeadXwtDtoMapper.toDto(head);
                return dto;
            }).collect(Collectors.toList());
        }
        return dcBillHeadXwtDtos;
    }

    /**
     * 功能描述:获取清单状态信息
     * @return
     */

    public List<DcBillCodeItemDto> getStatus() {
        return  dcBillHeadXwtMapper.getSatus();
    }

    /**
     * 功能描述:获取报关单状态信息
     * @return
     */
    public List<DcBillCodeItemDto> getEntryStatus() {
        return dcBillHeadXwtMapper.entryStatus();
    }

    /**
     * 功能描述:获取核扣标志信息
     * @return
     */
    public List<DcBillCodeItemDto> getVrfdedMark() {
        return dcBillHeadXwtMapper.getVrfdedMark();
    }
    /**
     * 功能描述:获取清单类型信息
     * @return
     */
    public List<DcBillCodeItemDto> getListType() {
        return dcBillHeadXwtMapper.listType();
    }
    /**
     * 功能描述:获取料件成品标志信息
     * @return
     */
    public List<DcBillCodeItemDto> getGMark() {
        return dcBillHeadXwtMapper.GMark();
    }
    /**
     * 功能描述:获取报关标志信息
     * @return
     */
    public List<DcBillCodeItemDto> getDclcusMark() {
        return dcBillHeadXwtMapper.dclcusMark();
    }
    /**
     * 功能描述:获取报关类型信息
     * @return
     */
    public List<DcBillCodeItemDto> getDclcusType() {
        return dcBillHeadXwtMapper.dclcusType();
    }
    /**
     * 功能描述:获取报关单类型信息
     * @return
     */
    public List<DcBillCodeItemDto> getEntryType() {
        return dcBillHeadXwtMapper.entryType();
    }
    /**
     * 功能描述:获取归并类型信息
     * @return
     */
    public List<DcBillCodeItemDto> getMergeType() {
        return dcBillHeadXwtMapper.mergeType();
    }

    /**
     * 验证同一手(账)册号下企业编号是否重复
     * @param dcBillHeadXwtParam
     * @return
     */
    public int getCount(DcBillHeadXwtParam dcBillHeadXwtParam) {
        return dcBillHeadXwtMapper.count(dcBillHeadXwtParam);
    }

    //TODO 建议简单参数的方法的Param写小，尽量不要用大Param 比如DcBillHeadXwtParam

    /**
     * 校验删除记录表(t_dc_bill_head_his)中pre_seq_no不为空的内部编号是否重复
     * @param dcBillHeadXwtParam
     * @return
     */
    public int getEmsListNoCount(DcBillHeadXwtParam dcBillHeadXwtParam) {
        return dcBillHeadXwtMapper.emsListNoCount(dcBillHeadXwtParam);
    }
    /**
     * 查询报关单申报单位名称和社会信用代码
     * @param dcBillHeadXwtDto
     * @return
     */
    public DcBillHeadXwtDto getAgentNameAndCreditCode(DcBillHeadXwtDto dcBillHeadXwtDto) {
        return dcBillHeadXwtMapper.agentNameAndCreditCode(dcBillHeadXwtDto);
    }
    /**
     * 功能描述:查询流转类型信息
     * @return
     */
    public List<DcBillCodeItemDto> getRotateType() {
        return dcBillHeadXwtMapper.rotateType();
    }

    /**
     * 根据当前账册号查询经营单位,加工单位,录入单位的编码,名称和社会信用代码
     * @param dcBillHeadXwtParam
     * @return
     */
    public EmsCusHeadCurDto getCodeNameCreditCodeByEmsNo(DcBillHeadXwtParam dcBillHeadXwtParam) {
        return dcBillHeadXwtMapper.codeNameCreditCodeByEmsNo(dcBillHeadXwtParam);
    }

    /**
     * 查询序列下一个值
     * @return
     */
    public long getSBillListNoXwtDto() {
        return dcBillHeadXwtMapper.sBillListNoXwtDto();
    }

    /**
     * 表头删除后添加
     * @param dcBillDeleteLogParam
     * @return
     */
    public int insertDcBillDeleteLog(DcBillDeleteLogParam dcBillDeleteLogParam) {
        return dcBillHeadXwtMapper.insertDcBillDeleteLog(dcBillDeleteLogParam);
    }

    /**
     * 删改申请判断是否备份
     * @param dcBillHeadXwtParam
     * @return
     */
    public int getHeadHis(DcBillHeadXwtParam dcBillHeadXwtParam) {
        return dcBillHeadXwtMapper.getHeadHis(dcBillHeadXwtParam);
    }

    /**
     * 备份表头数据
     * @param dcBillHeadXwtParam
     * @return
     */
    public int insertHeadHis(DcBillHeadXwtParam dcBillHeadXwtParam) {
        return dcBillHeadXwtMapper.insertHeadHis(dcBillHeadXwtParam);
    }
    /**
     * 备份表体数据
     * @param dcBillHeadXwtParam
     * @return
     */
    public int insertListHis(DcBillHeadXwtParam dcBillHeadXwtParam) {
        return dcBillHeadXwtMapper.insertListHis(dcBillHeadXwtParam);
    }

    /**
     * 修改状态和清单申报信息
     * @param dcBillHeadXwtParam
     */
    public int updList(DcBillHeadXwtParam dcBillHeadXwtParam,UserInfoToken userInfo) {
        //修改状态和清单申报信息
        dcBillHeadXwtParam.setSendUser(userInfo.getUserNo());
        dcBillHeadXwtParam.setAgentCreditCode(userInfo.getSocialCreditCode());
        dcBillHeadXwtParam.setAgentName(userInfo.getCompanyName());
        return dcBillHeadXwtMapper.updList(dcBillHeadXwtParam);
    }

    /**
     * 删改申请:恢复
     * @param dcBillHeadXwtParam
     * @return
     */
    public int recoveryData(DcBillHeadXwtParam dcBillHeadXwtParam) {
        return dcBillHeadXwtMapper.recoveryData(dcBillHeadXwtParam);
    }

    /**
     * 清空表体数据
     * @param dcBillHeadXwtParam
     * @return
     */
    public int deleteList(DcBillHeadXwtParam dcBillHeadXwtParam) {
        return dcBillHeadXwtMapper.deleteList(dcBillHeadXwtParam);
    }

    /**
     * 获取当前表头的表体数据的条数
     * @param dcBillHeadXwtParam
     * @return
     */
    public int getListCount(DcBillHeadXwtParam dcBillHeadXwtParam) {
        return dcBillHeadXwtMapper.getListCount(dcBillHeadXwtParam);
    }
    /**
     * 删除表头数据,根据billListNo
     * @param dcBillHeadXwtParam
     * @return
     */
    public int delHeadListByBillListNo(DcBillHeadXwtParam dcBillHeadXwtParam) {
        return dcBillHeadXwtMapper.delHeadListByBillListNo(dcBillHeadXwtParam);
    }

    /**
     * 从备份表里恢复数据
     * @param dcBillHeadXwtParam
     * @return
     */
    public int insertHeadList(DcBillHeadXwtParam dcBillHeadXwtParam) {
        return dcBillHeadXwtMapper.insertHeadList(dcBillHeadXwtParam);
    }
}
